package MtG.client.panels;

import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JSeparator;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import MtG.client.ClientConnection;
import MtG.client.GUI;
import MtG.publicClass.Game;
import MtG.publicClass.Player;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * InfoPanel.java is the class for making the info panel on the right side of the GUI.
 * @author Marek Sirp
 * @author Jaan Sepp
 */
public class InfoPanel extends JPanel{
	
	/**
	 * Determines if a de-serialized file is compatible with this class.
	 */
	private static final long serialVersionUID = 9208987816814365896L;
	//Your information panel components.
	private JLabel player1Info = new JLabel("Player 1 info :");		
	private JSeparator separator = new JSeparator();
	private JLabel player1Name = new JLabel("Name");		
	private JLabel player1Life = new JLabel("HP");
	private JLabel player1Deck = new JLabel("Deck");
	private JLabel player1NameValue = new JLabel("yourName");		
	private JLabel player1LifeValue = new JLabel("20");		
	private JLabel player1DeckValue = new JLabel("yourDeck");	
	private JLabel player1Mana = new JLabel("Mana");
	private JLabel player1ManaValue = new JLabel("0/0");
	
	//Opponent information panel components.
	private JLabel player2Info = new JLabel("Player 2 info:");			
	private JSeparator separator_1 = new JSeparator();		
	private JLabel player2Name = new JLabel("Name");		
	private JLabel player2Life = new JLabel("HP");		
	private JLabel player2Deck = new JLabel("Deck");			
	private JLabel player2NameValue = new JLabel("oppName");	
	private JLabel player2LifeValue = new JLabel("20");		
	private JLabel player2DeckValue = new JLabel("oppDeck");
	private JLabel player2Mana = new JLabel("Mana");		
	private JLabel player2ManaValue = new JLabel("0/0");
	
	//End turn button
	private JButton endTurnButton = new JButton("End Turn");
	
	/**
	 * Constructor.
	 */
	public InfoPanel(){
		setName("InfoPanel");
		setBackground(Color.GREEN);		
		
		//Panel layout
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(10)
					.addComponent(player1Info)
					.addGap(297))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(57)
					.addComponent(endTurnButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGap(223))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(player1Name)
						.addComponent(player1Life)
						.addComponent(player2Life)
						.addComponent(player2Name))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(player1LifeValue)
						.addComponent(player1NameValue)
						.addComponent(player2LifeValue)
						.addComponent(player2NameValue))
					.addContainerGap(251, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(player2Info)
					.addContainerGap(282, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(player1Mana)
						.addComponent(player1Deck))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(player1ManaValue)
						.addComponent(player1DeckValue))
					.addContainerGap(256, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(player2Deck)
						.addComponent(player2Mana))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(player2ManaValue)
						.addComponent(player2DeckValue))
					.addContainerGap(260, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
						.addComponent(separator, Alignment.LEADING)
						.addComponent(separator_1, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE))
					.addContainerGap(149, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(player1Info)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(separator, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player1Name)
						.addComponent(player1NameValue))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player1Life)
						.addComponent(player1LifeValue))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player1Mana)
						.addComponent(player1ManaValue))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player1Deck)
						.addComponent(player1DeckValue))
					.addGap(18)
					.addComponent(player2Info)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(separator_1, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player2Name)
						.addComponent(player2NameValue))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player2Life)
						.addComponent(player2LifeValue))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player2Mana)
						.addComponent(player2ManaValue))
					.addGap(8)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(player2Deck)
						.addComponent(player2DeckValue))
					.addPreferredGap(ComponentPlacement.RELATED, 252, Short.MAX_VALUE)
					.addComponent(endTurnButton)
					.addContainerGap())
		);
		setLayout(groupLayout);
		
		//End turn button listener
		endTurnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent action) {
				GUI gui = (GUI)getTopLevelAncestor();
				ClientConnection cc = gui.getClientConnection();	
				if(cc != null && cc.getGame().getWhoseTurn().getSecurityNumber().equals(cc.getRealPlayer().getSecurityNumber())){
					cc.switchplayers();	
				}
			}
		});
	}
	
	
	/**
	 * @param player player, whose mana is to be restored upon ending turn.
	 * @param roll String that holds either "player1" or "player2".
	 */
	public void updateOnTurn(Player player, String roll){
		String result = player.getInitMana() + "/" + player.getMaxMana();
		if(roll.equals("player1")){
			player1ManaValue.setText(result);
		} else {
			player2ManaValue.setText(result);
		}

	}
	
	/**
	 * @param game the reference to the game based on which the stats are updated on the info panel.
	 */
	public void updateStats(Game game){
		player1NameValue.setText(game.getPlayer1().getName());
		player2NameValue.setText(game.getPlayer2().getName());
		player1DeckValue.setText(game.getPlayer1().getDeck().getClass().getSimpleName());
		player2DeckValue.setText(game.getPlayer2().getDeck().getClass().getSimpleName());
		player1LifeValue.setText(Integer.toString((game.getPlayer1().getCurrentHitPoints())));
		player2LifeValue.setText(Integer.toString((game.getPlayer2().getCurrentHitPoints())));
		player1ManaValue.setText(game.getPlayer1().getInitMana() + "/" + game.getPlayer1().getMaxMana());
		player2ManaValue.setText(game.getPlayer2().getInitMana() + "/" + game.getPlayer2().getMaxMana());
	}
}
